package taskmaster.job;

import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.*;
//import taskmaster.auth.User;


/**taskmaster.job.QueryParams
 *<BR><BR>
 *A storage object for the parameters that TaskBuilder should use when creating a TaskCollection.
 *<BR><BR>
 *Instantiated by ViewTasks.java with the User's form data from viewTasks.jsp.
 *This object is stored in ViewTasks.java and TaskBuilder.java as a record of what set of criteria
 *were used to create that TaskCollection.
 *
 *@see taskmaster.controller.ViewTasks.getTaskCollection()
 */

/**
 * taskmaster.job.QueryParams
 * <BR><BR>
 * A storage object for the parameters that TaskBuilder should use when creating a TaskCollection.
 * <BR><BR>
 * Instantiated by ViewTasks.java with the User's form data from viewTasks.jsp.
 * This object is stored in ViewTasks.java and TaskBuilder.java as a record of what set of criteria
 * were used to create that TaskCollection.
 */
public class QueryParams implements java.io.Serializable
{

    /**
     * the roles that this Collection should be limited to, if any.
     */
    private final int[] limitToRole;
    /**
     * the jobs that this Collection should be limited to, if any.
     */
    private final String[] limitToJob;
    /**
     * the categories that this collection should be limited to, if any.
     */
    private final Set<String> limitToCategory;
    /**
     * the types of Tasks that this Collection should be limited to, if any.
     */
    private final char[] limitToType;
    /**
     * the run date that this collection should reflect.  Cannot be null.
     */
    private final Date date;
    /**
     * whether or not the collection should include deleted tasks; default false.
     */
    private final boolean showDeleted;
    private static final Format FORMAT = new SimpleDateFormat("E MMM-dd-yy");

    public QueryParams(int[] limitToRole,
                       String[] limitToJob,
                       Set<String> limitToCategory,
                       char[] limitToType,
                       Date date,
                       boolean showDeleted)
    {
        this.limitToRole = limitToRole;
        this.limitToJob = limitToJob;
        if (limitToCategory == null)
        {
            this.limitToCategory = new HashSet<String>();
        } else
        {
            this.limitToCategory = limitToCategory;
        }
        this.limitToType = limitToType;
        this.date = date;
        this.showDeleted = showDeleted;
    }

    /**
     * @return the internal field limitToRole
     */
    public int[] getLimitToRole()
    { return this.limitToRole; }

    /**
     * @return the internal field limitToJob
     */
    public String[] getLimitToJob()
    { return this.limitToJob; }

    /**
     * @return the internal field limitToCategory
     */
    public Set<String> getLimitToCategory()
    { return this.limitToCategory; }

    /**
     * @return the internal field limitToType
     */
    public char[] getLimitToType()
    { return this.limitToType; }

    /**
     * @return the internal field for run_date (date)
     */
    public Date getDate()
    { return this.date; }

    /**
     * @return the internal field for whether or not to show deleted tasks
     */
    public boolean getShowDeleted()
    { return this.showDeleted; }

    /**
     * @return true if the internal field date that denotes the run date for this collection is the calendar day today.
     */
    public boolean dateIsToday()
    {
        Date today = new Date();
        Calendar todayC = Calendar.getInstance();
        todayC.setTime(today);

        Calendar runC = Calendar.getInstance();
        runC.setTime(this.date);

        if (todayC.get(Calendar.DAY_OF_YEAR) == runC.get(Calendar.DAY_OF_YEAR) &&
                todayC.get(Calendar.YEAR) == runC.get(Calendar.YEAR))
        {
            return true;
        }
        return false;
    }

    @Override
    public String toString()
    {
        return "QueryParams{" +
                "limitToRole=" + Arrays.toString(limitToRole) +
                ", limitToJob=" + (limitToJob == null ? null : Arrays.asList(limitToJob)) +
                ", limitToCategory=" + limitToCategory +
                ", limitToType=" + Arrays.toString(limitToType) +
                ", date=" + date +
                ", showDeleted=" + showDeleted +
                '}';
    }

    public String getDisplayString()
    {
        StringBuilder sb = new StringBuilder("QueryParams");
        sb.append(":date=").append(FORMAT.format(date));
        if (limitToRole != null && limitToRole.length > 0)
        {
            sb.append(":Role(s)=").append(Arrays.toString(limitToRole));
        }
        if (limitToJob != null && limitToJob.length > 0)
        {
            sb.append(":Job(s)=").append(Arrays.asList(limitToJob));
        }
        if (limitToCategory != null && limitToCategory.size() > 0)
        {
            sb.append(":limitToCategory=");
            if (limitToCategory.size() == 1)
            {
                sb.append(limitToCategory.iterator().next());
            } else
            {
                for (String s : limitToCategory)
                {
                    sb.append(",").append(s);
                }
            }
        }
        if (limitToType != null && limitToType.length > 0)
        {
            sb.append(":limitToType=").append(Arrays.toString(limitToType));
        }

        return sb.toString();
    }
}
